/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sisgraf.controler;
import sisgraf.dao.MaterialDaoImp;
import sisgraf.model.Material;
import java.util.List;
import javax.swing.table.TableModel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
/**
 *
 * @author Jordan Guerra
 */
public class MaterialControler extends Controler {
    private MaterialDaoImp daoLock = new MaterialDaoImp();
    public TableModel getListarMaterial(String par) {
        List<Material> lista = null;
        if (par != null)
            lista = new MaterialDaoImp().list(par);
        else
            lista = new MaterialDaoImp().list("m.id = 0");
        String[] cabecalho = new String[]  {"Cod","Descricao","Unidade",
            "Cor","Custo","Tipo","Larg.","Comp.","Gram.",
            "Rend.","Ativo"};
        TableModel modelo = new MaterialTabela(lista, cabecalho);
        return modelo;
    }
    public void ajustarTabela(JTable tabela){
        DefaultTableCellRenderer e = new DefaultTableCellRenderer();
        DefaultTableCellRenderer c = new DefaultTableCellRenderer();
        DefaultTableCellRenderer d = new DefaultTableCellRenderer();
        e.setHorizontalAlignment(SwingConstants.LEFT);
        c.setHorizontalAlignment(SwingConstants.CENTER);
        d.setHorizontalAlignment(SwingConstants.RIGHT);
        int[] tamCol = new int[] {6,50,10,10,15,7,7,7,7,7,7};
        DefaultTableCellRenderer[] aliCol = new DefaultTableCellRenderer[] {d,e,e,e,d,e,d,d,d,d,e};
        super.ajustarTabela(tabela, tamCol, aliCol);
    }
    public Material prepararAlterarMaterial(int id){
        return daoLock.getLockMaterial(id);
    }
    public boolean excluirMaterial(Material material) {
        MaterialDaoImp dao = new MaterialDaoImp();
        return dao.remove(material);
    }
    public boolean adicionarMaterial(Material material){
        MaterialDaoImp dao = new MaterialDaoImp();
        return dao.save(material);
    }

    public boolean alterarMaterial(Material material){
        if (!daoLock.settLockMaterial(material)){
            return false;
        } else {
            return true;
        }
    }
    public boolean liberarMaterial(){
        if (!daoLock.unLockMaterial()){
            return false;
        } else {
            return true;
        }
    }

}
